package com.hrzyit.whistle.cases.ui.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import com.hrzyit.base.ui.activity.BaseActivity
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.common.CaseConstant
import com.hrzyit.whistle.cases.ui.fragment.IndexDeptWhistleFragment
import com.hrzyit.whistle.cases.ui.fragment.SelectDeptAndUserForWhistleFragment
import java.util.*

/**
 * 吹哨功能选择单位和人员 Activity, 不同选择单位和人员 Fragment 界面的壳
 */
class WhistleDeptAndUserActivity : BaseActivity() {

    // Fragment 栈
    private var mStack: Stack<Fragment> = Stack()

    /**
     * 吹哨单位选择界面 Fragment
     */
    private val selectDeptAndUserForWhistleFragment: SelectDeptAndUserForWhistleFragment by lazy {
        SelectDeptAndUserForWhistleFragment()
    }

    /**
     * 吹哨单位选择界面 Fragment
     */
    private val indexDeptWhistleFragment: IndexDeptWhistleFragment by lazy {
        IndexDeptWhistleFragment()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_whistle_dept_and_user)

        // 获取显示的 Fragment 标签
        val tag = intent.getStringExtra("tag")

        initFragment()

        changeFragmentByTag(tag)
    }

    /**
     * 初始化Fragment栈管理
     */
    private fun initFragment() {
        val manager = supportFragmentManager.beginTransaction()

        manager.add(
            R.id.mWhistleSelectDeptAndUserCt,
            selectDeptAndUserForWhistleFragment,
            CaseConstant.TAG_FRAGMENT_FOR_WHISTLE
        )
        manager.add(
            R.id.mWhistleSelectDeptAndUserCt,
            indexDeptWhistleFragment,
            CaseConstant.TAG_FRAGMENT_DEPT_INDEX_FOR_WHISTLE
        )
        manager.commit()

        mStack.add(selectDeptAndUserForWhistleFragment)
        mStack.add(indexDeptWhistleFragment)
    }

    /**
     * 通过 Tag 切换 Fragment
     */
    private fun changeFragmentByTag(tag: String) {
        val manager = supportFragmentManager.beginTransaction()
        mStack.forEach{
            when (it.tag) {
                tag -> {
                    manager.show(it)
                }
                else -> {
                    manager.hide(it)
                }
            }
        }
        manager.commit()
    }
}
